import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;

public class ByteCode {
    public CtClass modifyByteCode(String className, String method) throws Exception {
        ClassPool classPool = ClassPool.getDefault();
        CtClass ctClass = classPool.get(className);
        CtMethod oldMethod = ctClass.getDeclaredMethod(method);
        String oldMethodName = oldMethod.getName();
        String newName = oldMethodName + "$impl";
        oldMethod.setName(newName);

        CtMethod newMethod = CtNewMethod.copy(oldMethod, oldMethodName, ctClass, null);
        String sb = "{" +
                "\nSystem.out.println(\"start to modify bytecode\");\n" +
                "long start = System.currentTimeMillis();\n" +
                newName + "($$);\n" +
                "System.out.println(\"call method" + oldMethodName + "took\"+(System.currentTimeMillis()-start))" +
                "}";
        newMethod.setBody(sb);
        ctClass.addMethod(newMethod);
        return ctClass;
    }
}
